//
//  NoteViewController.m
//  Four_Fit_New
//
//  Created by lanou on 16/7/19.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "NoteViewController.h"
#import "comment.h"
#import "CommunityCell.h"
#import "CommentCell.h"
#import "NoteModel.h"
#import "ThumbButton.h"
#import <UIButton+WebCache.h>
#import "CommunityUserViewController.h"
#import "LikeInfoViewController.h"

@interface NoteViewController ()<UITableViewDelegate, UITableViewDataSource,UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 存放评论模型 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 存放动态模型 */
@property (nonatomic, strong) NSMutableArray *notArray;
/** 暂存noteModel */
@property (nonatomic, strong) NoteModel *noteModel;
/** 存放点赞用户头像网址 */
@property (nonatomic, strong) NSMutableArray *likeArray;
/** 存放点赞用户的id */
@property (nonatomic, strong) NSMutableArray *idArray;
/** 存放点赞用户的名字 */
@property (nonatomic, strong) NSMutableArray *nameArray;
/** 存放用户简介 */
@property (nonatomic, strong) NSMutableArray *declarationArray;
@end
static NSString *const cellID = @"Comment_cell";
@implementation NoteViewController

- (NSMutableArray *)notArray {
    if (!_notArray) {
        _notArray = [NSMutableArray array];
        
    }
    return _notArray;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)likeArray {
    if (!_likeArray) {
        _likeArray = [NSMutableArray array];
    }
    return _likeArray;
}
- (NSMutableArray *)idArray {
    if (!_idArray) {
        _idArray = [NSMutableArray array];
    }
    return _idArray;
}
- (NSMutableArray *)nameArray {
    if (!_nameArray) {
        _nameArray = [NSMutableArray array];
    }
    return _nameArray;
}
- (NSMutableArray *)declarationArray {
    if (!_declarationArray) {
        _declarationArray = [NSMutableArray array];
    }
    return _declarationArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:cellID];
    [self loadData];
    [self loadLikeUser];
    
    //接收点击评论用户头像的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTapCommentUserPicture:) name:@"didTapCommentUserPicture" object:nil];
}

#pragma mark 接收到评论用户头像被点击的通知，跳转到用户详情界面
- (void)didTapCommentUserPicture:(NSNotification *)note {
    
    CommunityUserViewController *userVc = [[CommunityUserViewController alloc] init];
    
    userVc.userID = note.userInfo[@"commentUserID"];
    userVc.userName = note.userInfo[@"userName"];
    
    [self.navigationController pushViewController:userVc animated:YES];
}

- (void)setUpHeaderView {
    
    UIView *header = [[UIView alloc] init];
    self.noteModel = self.notArray[0];
    CommunityCell *cell = [CommunityCell cell];
    //先调用noteModel的cellHeight的get方法，
    cell.size = CGSizeMake(kWidth, self.noteModel.cellHeight);
    //在传模型，否则模型里面cellHeight没值
    cell.noteModel = self.noteModel;
   
    [header addSubview:cell];
    
    header.height = cell.noteModel.cellHeight + 50;

    //点赞view
    UIView *likeView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.noteModel.cellHeight, kWidth, 40)];
    //    likeView.backgroundColor = [UIColor redColor];
    ThumbButton *likeBtn = [[ThumbButton alloc] init];
    likeBtn.frame = CGRectMake(20, 5, 30, 30);
    //点赞按钮被点击回调事件
    likeBtn.buttonClick = ^(){
        NSLog(@"按钮被点击回调");
    };
    [likeView addSubview:likeBtn];
    for (int i = 0; i < 5; i++) {
        //        if (i == 5) return;
        UIButton *userbtn = [[UIButton alloc] initWithFrame:CGRectMake(60 + 50 * i, 0, 40, 40)];
        userbtn.tag = i + 10;
        [userbtn addTarget:self action:@selector(clickUserImage:) forControlEvents:UIControlEventTouchUpInside];
        userbtn.layer.cornerRadius = 20;
        userbtn.layer.masksToBounds = YES;
        userbtn.backgroundColor = [UIColor redColor];
        if (self.likeArray.count > 0) {
            [userbtn sd_setImageWithURL:[NSURL URLWithString:self.likeArray[i]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"coolStart_female_n"]];
        }
        
        [likeView addSubview:userbtn];
        
    }
    UIButton *moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth - 60, 0, 40, 40)];
    [moreBtn addTarget:self action:@selector(lookMoreUser) forControlEvents:UIControlEventTouchUpInside];
    moreBtn.layer.cornerRadius = 20;
    moreBtn.layer.masksToBounds = YES;
    [moreBtn setImage:[UIImage imageNamed:@"find_other"] forState:UIControlStateNormal];
    [likeView addSubview:moreBtn];
    
 
    [header addSubview:likeView];
    self.tableView.tableHeaderView = header;
    
}

- (void)clickUserImage:(UIButton *)btn {
    
    NSLog(@"%ld",btn.tag - 10);
    CommunityUserViewController *userVc = [[CommunityUserViewController alloc] init];
    userVc.userID = self.idArray[btn.tag - 10];
    userVc.userName = self.nameArray[btn.tag - 10];
    
    [self.navigationController pushViewController:userVc animated:YES];
    
}

- (void)lookMoreUser {
    
    LikeInfoViewController *likeVc = [[LikeInfoViewController alloc] init];
    likeVc.nameArray = self.nameArray;
    likeVc.declarationArray = self.declarationArray;
    likeVc.urlArray = self.likeArray;
    likeVc.idArray = self.idArray;
    [self.navigationController pushViewController:likeVc animated:YES];
}

- (void)loadData {
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"tokenid"] = @"tokenid_undefined";
    parame[@"userid"] = @"5784da4c4925c70c581758a9";
    parame[@"noteid"] = self.noteid;
    parame[@"time"] = @"1468831382008";
    
    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://ws.sythealth.com/ws/fit/newforum/note/getnoteandcommbyid" parameters:parame method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"%@",dic[@"data"]);
        
        for (NSDictionary *commentDic in dic[@"data"][@"items"]) {
            comment *cot = [[comment alloc] init];
            [cot setValuesForKeysWithDictionary:commentDic];
            [self.dataArray addObject:cot];
            NSLog(@"test");
        }
        
            NoteModel *note = [[NoteModel alloc] init];
            [note setValuesForKeysWithDictionary:dic[@"data"]];
            [self.notArray addObject:note];
        
        NSLog(@"noteArray----->%@",self.notArray);
        
    } updateUI:^{
    
        [self setUpHeaderView];
        [self.tableView reloadData];
    }];
    
}

- (void)loadLikeUser {
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"feedid"] = self.noteid;
    parame[@"tokenid"] = @"tokenid_undefined";
    parame[@"page"] = @1;
    parame[@"pagesize"] = @9;
    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://ws.sythealth.com/ws/fit/v4_3/feed/getpraiseusers" parameters:parame method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        //        NSLog(@"%@",dic);
        
        for (NSDictionary *dict in dic[@"data"]) {
            
            if (dict[@"url"]) {
                [self.likeArray addObject:dict[@"url"]];
                [self.idArray addObject:dict[@"userid"]];
                [self.nameArray addObject:dict[@"name"]];
                [self.declarationArray addObject:dict[@"declaration"]];
            }
            
        }
        
    } updateUI:^{
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.comment = self.dataArray[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    comment *com = self.dataArray[indexPath.row];
    
    return com.cellHeight;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
